function build_error_html(msg) {
	return '<div class="alert alert-block alert-error"><a href="#" class="close" data-dismiss="alert">&times;</a><p>' + msg + '</p></div>';
}

function init_signin_form() {
	var error_msg = $('#form-signin .err-board');
	var btn_submit = $('#form-signin button[type="submit"]');
	$('#form-signin').submit(function() {
		error_msg.hide(); btn_submit.button('loading');
		$.post(
			$(this).attr('action'), $(this).serialize(),
			function(ret) {
				btn_submit.button('reset');
				if (ret.status == page.consts.status_ok) {
					window.location.href = ret.data;
				} else {
					error_msg.html(build_error_html(ret.msg)); error_msg.show();
				}
			},
			'json'
		);
		return false;
	});
}

function init_signup_form() {
	var chk_agree = $('#form-signup input[type="checkbox"]');
	chk_agree.prop('checked', false);
	chk_agree.change(function() {
		if ($(this).is(':checked')) {
			$('#form-signup button[type="submit"]').removeAttr('disabled');
		} else {
			$('#form-signup button[type="submit"]').attr('disabled', 'disabled');
		}
	});
	var error_msg = $('#form-signup .err-board');
	$('#form-signup').submit(function() {
		error_msg.hide();
		$.post(
			$(this).attr('action'), $(this).serialize(),
			function(ret) {
				if (ret.status == page.consts.status_ok) {
					window.location.href = ret.data;
				} else {
					error_msg.html(build_error_html(ret.msg)); error_msg.show();
				}
			},
			'json'
		);
		return false;
	});
}

function init_post_submit_form() {
	var error_msg = $('#form-post-submit .err-board');
	var btn_submit = $('#form-post-submit button[type="submit"]');
	$('#form-post-submit').submit(function() {
		error_msg.hide(); btn_submit.button('loading');
		$.post(
			$(this).attr('action'), $(this).serialize(),
			function(ret) {
				btn_submit.button('reset');
				if (ret.status == page.consts.status_ok) {
					//alert(ret.msg);
					window.location.href = ret.data;
				} else {
					error_msg.html(build_error_html(ret.msg)); error_msg.show();
				}
			},
			'json'
		);
		return false;
	});
}

function init_comment_form() {
	$('.media-body button').tooltip();
	var error_msg = $('#form-post-comment .err-board');
	var btn_submit = $('#form-post-comment button[type="submit"]');
	var alert_no_comment = $('#alert-no-comment');
	var comment_panel = $('#comment-panel');
	if ($('.media', comment_panel).length <= 0) {
		comment_panel.hide();
		alert_no_comment.show();
	} else {
		alert_no_comment.hide();
		comment_panel.show();
	}
	$('#form-post-comment').submit(function() {
		var form = this;
		error_msg.hide(); btn_submit.button('loading');
		$.post(
			$(this).attr('action'), $(this).serialize(),
			function(ret) {
				btn_submit.button('reset');
				if (ret.status == page.consts.status_ok) {
					form.reset();
					var node = $(ret.data);
					comment_panel.append(node);
					Holder.run();
					$('.media-body button', node).tooltip();
					if (comment_panel.is(':hidden')) {
						alert_no_comment.fadeOut();
						comment_panel.fadeIn();
					}
				} else {
					error_msg.html(build_error_html(ret.msg)); error_msg.show();
				}
			},
			'json'
		);
		return false;
	});
}

function init_comment_actions() {
	$('#comment-panel').delegate('.comment-actions button:nth-child(1)', 'click', function() {
		var comment_id = $(this).parent().parent().parent().attr('cid');
		var btn = this;
		$.post(
			page.urls.vote_up_comment, {'comment_id': comment_id},
			function(ret) {
				if (ret.status == page.consts.status_ok) {
					var good_span = $('span', btn);
					var good_count = parseInt(good_span.text()) + 1;
					good_span.text(good_count);
				} else {
					alert(ret.msg);
				}
			}, 'json'
		);
	});
	$('#comment-panel').delegate('.comment-actions button:nth-child(2)', 'click', function() {
		var comment_id = $(this).parent().parent().parent().attr('cid');
		$('#dlg-post-comment-reply input[name="reply_id"]').val(comment_id);
		$('#dlg-post-comment-reply').modal('show');
	});
	$('#comment-panel').delegate('.comment-actions button:nth-child(3)', 'click', function() {
		var root_node = $(this).parent().parent().parent();
		var comment_id = root_node.attr('cid');
		$.post(
			page.urls.del_comment, {'comment_id': comment_id},
			function(ret) {
				if (ret.status == page.consts.status_ok) {
					root_node.remove();
					if ($('#comment-panel .media').length <= 0) {
						$('#comment-panel').fadeOut();
						$('#alert-no-comment').fadeIn();
					}
				} else {
					alert(ret.msg);
				}
			}, 'json'
		);
	});
}

function init_reply_dialog() {
	var btn_submit = $('#dlg-post-comment-reply .modal-footer button:first');
	btn_submit.click(function() { $('#dlg-post-comment-reply form').submit(); });
	$('#dlg-post-comment-reply form').submit(function() {
		var form = this;
		var error_msg = $('.err-board', this);
		error_msg.hide(); btn_submit.button('loading');
		var reply_id = $('input[name="reply_id"]', this).val();
		$.post(
			$(this).attr('action'), $(this).serialize(),
			function(ret) {
				btn_submit.button('reset');
				if (ret.status == page.consts.status_ok) {
					form.reset();
					$('#dlg-post-comment-reply').modal('hide');
					var node = $(ret.data);
					$('.media[cid="' + reply_id + '"] > .media-body').append(node);
					Holder.run();
					$('.media-body button', node).tooltip();
				} else {
					error_msg.html(build_error_html(ret.msg)); error_msg.show();
				}
			},
			'json'
		);
		return false;
	});
}

function init_vote_actions() {
	$('.vote-actions button').click(function() {
		var post_id = $(this).parent().parent().attr('data-pid');
		var choice_num = $(this).attr('data-cnum');
		var btn_vote = $(this);
		btn_vote.button('loading');
		$.post(
			page.urls.vote_choice, {'post_id': post_id, 'choice_num': choice_num},
			function(ret) {
				btn_vote.button('reset');
				if (ret.status == page.consts.status_ok) {
					$('.vote-actions').fadeOut();
					show_vote_result();
				} else {
					alert(ret.msg);
				}
			},
			'json'
		);
	});
	if (page.page_info.is_voted) { $('.vote-actions').hide(); show_vote_result(); } else $('.result-actions').hide();
}

function show_vote_result() {
	$.post(
		page.urls.show_vote_result, {'post_id': $('.hero-unit').attr('data-pid')},
		function(ret) {
			if (ret.status == page.consts.status_ok) {
				$('#vote-result').html(ret.data);
			} else {
				alert(ret.msg);
			}
		},
		'json'
	);
}

$(function() {
	$('#navbar-nav-' + page.page_info.cur_nav_index).addClass('active');
	init_signin_form();
	init_signup_form();
	init_post_submit_form();
	init_comment_actions();
	init_comment_form();
	init_reply_dialog();
	init_vote_actions();
});
